/**
 * 定点搬运者
 */

let baser = require("./baser");

// 返回baser的内部生命函数
module.exports = function (sourceids, targetids, resourcetypes) {
    return baser(
        /**
         * 准备函数，确认下标
         * @param {Creep} creep
         */
        function (creep) {
            if (!creep.id) {
                return 1;
            }
            if (!creep.room.memory.carrierid) {
                creep.room.memory.carrierid = [];
            }
            for (let i = 0; i < sourceids.length; i++) {
                // 没有该下标对应的单位
                if (!Game.getObjectById(creep.room.memory.carrierid[i])) {
                    creep.room.memory.carrierid[i] = creep.id;
                    creep.memory.carrierindex = i;
                    return 2;
                }
            }
            return 1;
        },
        /**
         * 工作函数
         * @param {Creep} creep
         */
        function (creep) {
            // 装货
            let source = Game.getObjectById(sourceids[creep.memory.carrierindex]);
            if (source) {
                if (creep.withdraw(source, resourcetypes[creep.memory.carrierindex]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(source, { visualizePathStyle: { stroke: 'rgb(18, 230, 28)' } });
                }
            }
            // 卸货
            let target = Game.getObjectById(targetids[creep.memory.carrierindex]);
            if (target) {
                if (creep.transfer(target, resourcetypes[creep.memory.carrierindex]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(target, { visualizePathStyle: { stroke: 'rgb(18, 230, 28)' } });
                }
            }
            return 2;
        },
        /**
         * 工作函数，卸货
         * @param {Creep} creep
         */
        function (creep) {
            // 准备装货
            if (!creep.store[RESOURCE_ENERGY]) {
                return 2;
            }
            return 2;
        });
}